namespace SimpleWebServices.Web.UI.Html.Validation
{
    using System;
    using System.Collections.Generic;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Web.UI.Html.Elements;
    using Xunit;

    public sealed class StandardTextValidatorFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<StandardTextValidator>()
                .DerivesFrom<Validator>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new StandardTextValidator());
        }

        [Fact]
        public void ctor_IInnerElements_int_int()
        {
            Assert.NotNull(new StandardTextValidator(new Label(), 1, 2));
        }

        [Fact]
        public void ctor_IInnerElementsNull_int_int()
        {
            Assert.NotNull(new StandardTextValidator(null as IInnerElements, 1, 2));
        }

        [Fact]
        public void prop_Display()
        {
            Assert.True(new PropertyExpectations(typeof(StandardTextValidator).GetProperty("Display"))
                .IsAutoProperty<IInnerElements>()
                .Result);
        }

        [Fact]
        public void prop_Errors()
        {
            Assert.True(new PropertyExpectations(typeof(StandardTextValidator).GetProperty("Errors"))
                .DefaultValueIsNotNull()
                .TypeIs<IList<IError>>()
                .Result);
        }

        [Fact]
        public void prop_ErrorText()
        {
            Assert.True(new PropertyExpectations(typeof(StandardTextValidator).GetProperty("ErrorText"))
                .TypeIs<string>()
                .DefaultValueIsNotNull()
                .Result);
        }

        [Fact]
        public void prop_MaxLength()
        {
            Assert.True(new PropertyExpectations(typeof(StandardTextValidator).GetProperty("MaxLength"))
                .IsAutoProperty<int>()
                .Result);
        }

        [Fact]
        public void prop_MinLength()
        {
            Assert.True(new PropertyExpectations(typeof(StandardTextValidator).GetProperty("MinLength"))
                .IsAutoProperty<int>()
                .Result);
        }

        [Fact]
        public void op_Configure_Form_DataCollection()
        {
            DataCollection data = new DataCollection();
            data.Add("Display", "display");
            data.Add("ErrorText", "error");
            data.Add("MaxLength", "10");
            data.Add("MinLength", "5");

            Label label = new Label { Id = "display" };
            Form form = new Form
            {
                Elements = { label }
            };

            StandardTextValidator obj = new StandardTextValidator(null, 0, 0);
            obj.Configure(form, data);

            Assert.Same(label, obj.Display);
            Assert.Equal<string>("error", obj.ErrorText);
            Assert.Equal<int>(10, obj.MaxLength);
            Assert.Equal<int>(5, obj.MinLength);
        }

        [Fact]
        public void op_Configure_Form_DataCollectionEmpty()
        {
            StandardTextValidator obj = new StandardTextValidator(null, 0, 0);
            obj.Configure(new Form(), new DataCollection());

            Assert.Null(obj.Display);
            Assert.Equal<int>(0, obj.MaxLength);
            Assert.Equal<int>(0, obj.MinLength);
        }

        [Fact]
        public void op_Configure_Form_DataCollectionNull()
        {
            StandardTextValidator obj = new StandardTextValidator(null, 0, 0);
            obj.Configure(new Form(), null as DataCollection);

            Assert.Null(obj.Display);
            Assert.Equal<int>(0, obj.MaxLength);
            Assert.Equal<int>(0, obj.MinLength);
        }

        [Fact]
        public void op_Validate_IElementNotIControl()
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => new StandardTextValidator().Validate(new Anchor()));
        }

        [Fact]
        public void op_Validate_IElementNull()
        {
            Assert.Throws<ArgumentNullException>(() => new StandardTextValidator().Validate(null));
        }

        [Fact]
        public void op_Validate_IElementValid()
        {
            TextInput element = new TextInput
            {
                Value = "test"
            };

            StandardTextValidator obj = new StandardTextValidator(new Label(), 2, 10);

            Assert.True(obj.Validate(element));

            Assert.Equal<int>(0, obj.Errors.Count);
        }

        [Fact]
        public void op_Validate_IElementInvalidMinLength()
        {
            string error = "Error text";

            TextInput element = new TextInput
            {
                Value = "test"
            };

            StandardTextValidator obj = new StandardTextValidator(new Label(), 5, 10)
            {
                ErrorText = error
            };

            Assert.False(obj.Validate(element));

            Assert.Equal<int>(1, obj.Errors.Count);
            Assert.Equal<string>(error, obj.Errors[0].Description);
        }

        [Fact]
        public void op_Validate_IElementInvalidMaxLength()
        {
            string error = "Error text";

            TextInput element = new TextInput
            {
                Value = "test"
            };

            StandardTextValidator obj = new StandardTextValidator(new Label(), 1, 3)
            {
                ErrorText = error
            };

            Assert.False(obj.Validate(element));

            Assert.Equal<int>(1, obj.Errors.Count);
            Assert.Equal<string>(error, obj.Errors[0].Description);
        }
    }
}